package com.kuang.utils;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 项目启动 监听主题,重写run方法。springboot开启时自动监听
 *
 * @author zoe_zls
 * @since 2024/3/4
 */
@Slf4j
@Component
public class MQTTListener implements CommandLineRunner {

    @Resource
    MQTTConnect mqttConnect;
    @Value("${mqtt.user}")
    private String USER;
    @Value("${mqtt.password}")
    private String PASSWORD;

    @Override
    public void run(String... args) throws Exception {
        try {
            mqttConnect.setMqttClient(USER,PASSWORD, new Callback());
            String[] topics = {"/single/up/#", "/manage/up/#","/pfsi/up/#"};
            mqttConnect.subAll(topics);
            log.info("接收");
        } catch (MqttException e) {
            log.error(e.getMessage(), e);
        }
    }
}
 
 